classdef Utils < Singleton
    properties
        file_id = 0;
        should_print;
    end
    
    methods(Access = private)
        function obj = Utils()
            params = ParamsSingleton.instance();
            if (params.should_print_to_screen)
                obj.file_id = 1; % standard output
            else
                obj.file_id = fopen(params.output_file, 'w');
            end
            obj.should_print = params.should_print;
        end
    end
    
    methods(Static)
      function obj = instance(should_reinit)
         persistent unique_instance;
         should_reinit = (nargin>0);
         if isempty(unique_instance) || (should_reinit==true)
            unique_instance = Utils();
         end
         obj = unique_instance;
      end
   end
   
    methods
        function trace_no_tick(obj,str)
            fprintf(obj.file_id, str);
        end
        
        function trace(obj,str)
            ck = ClockSingleton.instance();
            fprintf(obj.file_id,'[tick %d]      ',ck.tick_count);
            fprintf(obj.file_id, str);
        end
        
        function destroy(obj)
            fclose(obj.file_id);
        end
    end
end